package cn.ekuma.epos.productsfinishing;

import java.util.List;
import java.util.Properties;

import com.openbravo.bean.AttributeValue;
import com.openbravo.bean.ProductAttributeValuePrice;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.panels.JObjectListFinder;

import cn.ekuma.data.ui.swing.AbstractChildDAOJEditor;
import cn.ekuma.epos.datalogic.I_DataLogicSales;

import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Dimension;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ProductAttributeValuePriceEditor extends AbstractChildDAOJEditor<ProductAttributeValuePrice,String> {
	private JTextField txtGroupName;
	private JTextField txtSalePrice;
	I_DataLogicSales dlSale;
	private JButton button;
	private JTextField txtSalePriceCost;
	public ProductAttributeValuePriceEditor(DirtyManager dirty,I_DataLogicSales dlSale) {
		super(dirty);
		this.dlSale=dlSale;
		setPreferredSize(new Dimension(351, 117));
		initComponents();
		txtGroupName.getDocument().addDocumentListener(dirty);
		txtSalePrice.getDocument().addDocumentListener(dirty);
	}
	private void initComponents() {
		setLayout(null);
		
		JLabel label = new JLabel();
		label.setBounds(48, 10, 138, 21);
		label.setText(AppLocal.getIntString("label.ProductPrice.groupName"));
		add(label);
		
		txtGroupName = new JTextField();
		txtGroupName.setHorizontalAlignment(SwingConstants.CENTER);
		txtGroupName.setBounds(196, 10, 109, 21);
		add(txtGroupName);
		
		JLabel label_1 = new JLabel();
		label_1.setText(AppLocal.getIntString("label.prodpricesell"));
		label_1.setBounds(48, 41, 144, 21);
		add(label_1);
		
		txtSalePrice = new JTextField();
		txtSalePrice.setHorizontalAlignment(SwingConstants.RIGHT);
		txtSalePrice.setBounds(196, 41, 111, 21);
		add(txtSalePrice);
		
	    button = new JButton("");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				choiceAttributeValue();
			}
		});
		button.setIcon(new ImageIcon(ProductAttributeValuePriceEditor.class.getResource("/com/openbravo/images/search.png")));
		button.setBounds(308, 9, 33, 23);
		add(button);
		
		JLabel label_2 = new JLabel(AppLocal.getIntString("label.prodpricebuy"));
		label_2.setBounds(48, 72, 138, 15);
		add(label_2);
		
		txtSalePriceCost = new JTextField();
		txtSalePriceCost.setBounds(196, 69, 109, 21);
		add(txtSalePriceCost);
		txtSalePriceCost.setColumns(10);
	}

	protected void choiceAttributeValue() {
		try {
			List attributeValues=dlSale.listAttributeValueByProduct(obj.getProductId());
			JObjectListFinder userList = JObjectListFinder.newJDialog(this);
			AttributeValue tempUser = (AttributeValue)userList.showUserList(attributeValues);
            if (tempUser != null) {
            	txtGroupName.setText(tempUser.getValue());
			}else
				txtGroupName.setText(null);
        } catch (BasicException ex) {
            MessageInf msg = new MessageInf(ex);
            msg.show(this);
        }
	}
	
	@Override
	public void refresh() {

	}

	@Override
	public String getTabName() {
		return AppLocal.getIntString("label.ProductAttributeValuePrice");
	}

	@Override
	public ProductAttributeValuePrice createNew(Properties prop) {
		ProductAttributeValuePrice obj=new ProductAttributeValuePrice();
		if(parentNode!=null)
			obj.setProductId(parentNode.getParentKey().getKey());
		return obj;
	}

	@Override
	public void rebuild(ProductAttributeValuePrice obj) throws BasicException {
		obj.setAttname(txtGroupName.getText());
		obj.setDiffprice((Double) Formats.CURRENCY.parseValue(txtSalePrice.getText()));
		obj.setDiffPriceCost((Double) Formats.CURRENCY.parseValue(txtSalePriceCost.getText()));	
	}

	@Override
	public void updateUI(boolean editAble, int state) {
		txtGroupName.setText(obj.getAttname());
		txtSalePrice.setText(Formats.CURRENCY.formatValue(obj.getDiffprice()));
		txtSalePriceCost.setText(Formats.CURRENCY.formatValue(obj.getDiffPriceCost()));
		txtGroupName.setEditable(editAble);
		txtSalePrice.setEditable(editAble);
		txtSalePriceCost.setEditable(editAble);
		button.setEnabled(editAble);
	}

	@Override
	public Class getEditClass() {
		return ProductAttributeValuePrice.class;
	}
}
